using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Deloitte.Windows.Forms;
using Deloitte.MSExcel;

namespace Deloitte.Windows.Forms
{
    public partial class ExcelNumberFormatMappingForm : Form
    {
        private FormatExcelCellsForm numberFormatForm;
        private MSExcel.NumberFormatMappingDictionary xlNumberFormatMapping;
        public ExcelNumberFormatMappingForm(string[] dataSourceNames)
        {
            InitializeComponent();
            this.xlNumberFormatMapping = new NumberFormatMappingDictionary();
            foreach (string dataSourceName in dataSourceNames)
            {
                this.xlNumberFormatMapping.Add(dataSourceName, Deloitte.MSExcel.ExcelHelper.ConvertDotNetTypeToNumberFormat(typeof(object)));
            }
            CommonConstructorCode();
        }
        public ExcelNumberFormatMappingForm(MSExcel.NumberFormatMappingDictionary numberFormats)
        {
            InitializeComponent();
            CommonConstructorCode();
        }
        public MSExcel.NumberFormatMappingDictionary XLNumberFormatMapping
        {
            get { return this.xlNumberFormatMapping; }
            set { this.xlNumberFormatMapping = value; }
        }
        private void CommonConstructorCode()
        {
            foreach (string column in this.xlNumberFormatMapping.Keys)            
            {
               DataGridViewRow dgvr = new DataGridViewRow();
                dgvr.CreateCells(this.dataGridView1);
                dgvr.Cells[0].Value = column;
                dgvr.Cells[1].Value = this.xlNumberFormatMapping[column];                
                this.dataGridView1.Rows.Add(dgvr);                
            }
            this.dataGridView1.Refresh();
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                if (numberFormatForm == null) numberFormatForm = new FormatExcelCellsForm();
                if (numberFormatForm.ShowDialog() == DialogResult.OK)
                {
                    this.dataGridView1["colExcelNumberFormat", e.RowIndex].Value = this.numberFormatForm.XLNumberFormat;                    
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            CreateMapping();        
        }
        private void CreateMapping()
        {
            if (this.XLNumberFormatMapping == null) { XLNumberFormatMapping = new NumberFormatMappingDictionary(); }
            foreach (DataGridViewRow dgrv in this.dataGridView1.Rows)
            {
                XLNumberFormatMapping.Add((string)dgrv.Cells["colColumnName"].Value, (string)dgrv.Cells["colExcelNumberFormat"].Value);
            }    
        }
    }
}